package com.chen;
/**
 * author chen
 * 解析dom元素
 * 昵图壁纸
 */

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class DownLoadImg {
    public static void main(String[] args) throws InterruptedException {


        String path = null;
        //设置名称
        int name=1;
        for (int i = 1; i <= 2; i++)//使用来控制页数
        {
            if (i == 1) {
                path = "https://sc.chinaz.com/tupian/huadetupian.html";
            } else {
                path = "https://sc.chinaz.com/tupian/huadetupian_" + i + ".html";
            }
            System.out.println(path);
            FileOutputStream outputStream=null;
            InputStream inputStream=null;
            try {
                //把HTML解析为document对象
                //把页面包装为url对象

                URL url = new URL(path);
                //通过jsouop解析器来解析页面，获得dom对象
                try {
                    Document document = Jsoup.parse(url, 30000);
                    //System.out.println(document);
                    //通过dom对象对应的标签名称获取数据

                    //得到大图页面
                    Elements elements = document.getElementsByClass("picblock");
                    for (Element element : elements) {
                        String href = "https:"+element.child(0).child(0).attr("href");
                        //System.out.println( href);
                        //把大图HTML页面解析为document对象
                        //把页面包装为url对象.通过jsoup解析器来解析页面...获得dom对象
                        URL hrefUrl = new URL(href);
                        Document document1 = Jsoup.parse(hrefUrl, 300000);//300000未解析出来会报错

                        //通过dom对象的方法找到指定的类名
                        Elements elements1 = document1.getElementsByClass("imga");
                        for (Element element1 : elements1) {
                            //选中指定标签通过.attr()方法获取属性的值
                            String imgSrc = "https:"+element1.child(0).child(0).attr("src");
                            System.out.println(imgSrc);
                            //打开链接
                            //创建url对象，把路径作为参数传入
                            URL imgsrcUrl = new URL(imgSrc);
                            //通过url对象的openConnection()方法，打开图片链接
                            URLConnection connection = imgsrcUrl.openConnection();
                            //下载图片
                            inputStream = connection.getInputStream();
                            //选择路径
                            String outFile="D:\\img\\"+name+".jpg";
                            name++;
                            //创建输出流
                            outputStream = new FileOutputStream(outFile);
                            byte[] bytes = new byte[1024];
                            int len = 0;
                            while((len=inputStream.read(bytes))!=-1){
                                outputStream.write(bytes,0,len);

                            }
                            Thread.sleep(3000);
                            System.out.println("下载完成");
                        }


                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            }finally {
                try {
                    inputStream.close();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }


}

